package www.bplead.com;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import www.bplead.com.data.DeviceData;
import www.bplead.com.printer.PrintService;
import www.bplead.com.test.TestBill;
import www.bplead.com.test.TestData;

public class MainActivity extends AppCompatActivity {

    private View printDeviceWrap;
    private Button printBtn;
    private TextView printerTv;
    private PrintService printService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setTitle("蓝牙打印");

        printerTv = (TextView) this.findViewById(R.id.printer_tv);
        printDeviceWrap = this.findViewById(R.id.print_device_wrap);
        printDeviceWrap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,PairedActivity.class);
                startActivity(intent);
            }
        });

        printBtn = (Button) this.findViewById(R.id.print_btn);
        printBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String testBillStr = TestBill.createBill(TestData.getData());
                if(printService != null){
                    printService.printTXT(testBillStr);
                }else{
                    Toast.makeText(MainActivity.this,"请配置打印设备",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        setCurrentPrinter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void setCurrentPrinter() {
        String device = DeviceData.getInstance(this).getPrinterDevice();
        if(!TextUtils.isEmpty(device)){
            String name = device.substring(0,device.indexOf("\n"));
            String mac = device.substring(device.indexOf("\n") + 1);
            printerTv.setText(name);
            printService = new PrintService(this,mac);
        }else{
            printerTv.setText("未设置");
        }
    }


}
